#-*- coding:utf-8 -*-
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import printer
import socket

K_SRV_IP        = '192.168.1.218'
K_SRV_PORT      = 6900

class TestHTTPHandle(BaseHTTPRequestHandler):
    def cmdSend(self, aCmd):
        try:
            s = socket.socket()
            s.connect((K_SRV_IP,K_SRV_PORT))
            s.sendall(aCmd)
            recv = s.recv(10240)
            s.close()
            return recv
        except socket.gaierror, e:
            print e
        except socket.error, e:
            print e
            
        return ''

    def cmdSplit(self, aName, aStr):
        if aStr == "<{true}[||]>" or aStr == "<{false}>":
            return ''
        tmpRet =aStr.split("|")[1]
        return tmpRet

    #打印信息
    def dbSelectGetOrderPrint(self) :
        tmpCmd = "<msg><cmd class='select' conname='' clientid='' istran=''><body>select F_YLW_YLW_MY_GET_ORDERPRINT()</body></cmd></msg>"
        tmpRet = self.cmdSend(tmpCmd)
        tmpRet = self.cmdSplit("dbSelectGetOrderPrint",tmpRet)
        return tmpRet

    def do_GET(self):
        self.protocal_version = "HTTP/1.1"
        self.send_response(200)  
        self.send_header("Welcome", "Contect")         
        self.end_headers()  
        #request db
        tmpRsp = self.dbSelectGetOrderPrint()
        if not tmpRsp or len(tmpRsp) <= 0:
            self.wfile.write('')
        else:
            self.wfile.write(printer.YLW_HttpPrinter_80(tmpRsp))

http_server = HTTPServer(('0.0.0.0', int(65420)), TestHTTPHandle)  
http_server.serve_forever()